package com.fita.admin.web.controller.authority;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.fita.admin.entity.security.Login;
import com.fita.admin.service.DataService;
import com.fita.admin.service.authority.RoleResourceService;
import com.fita.admin.web.resolver.Secure;
import com.fita.entity.authority.Role;
import com.fita.entity.authority.RoleResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 角色管理
 *
 * @author cuteHU
 *
 */
@Controller
@RequestMapping("/roles")
@MenuSetting("menu-authority")
public class RoleController {

    @Autowired
    @Qualifier("dataService")
    private DataService<Role> roleService;

    @Autowired
    private RoleResourceService roleResourceService;

    /**
     * @param login
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-role-management")
    public ModelAndView list(@Secure Login login, Page page) {

        ModelAndView modelAndView = new ModelAndView("authority/role/role-list");

        int totals = roleService.count(Role.class);
        if (totals > 0) {

            List<Role> roles = roleService.listByPage(Role.class, page.with(totals));

            modelAndView.addObject("roles", roles);
            modelAndView.addObject("page", page);
        }
        return modelAndView;
    }

    /**
     * 详情
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-role-management")
    public ModelAndView view(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("authority/role/role-view");

        Role role = roleService.load(Role.class, id);

        modelAndView.addObject("role", role);

        List<RoleResource> roleResources = roleResourceService.list(role);
        modelAndView.addObject("roleResources", roleResources);

        return modelAndView;
    }

}
